<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增模板</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="../lib/layui/css/layui.css">
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../css/xadmin.css">
    <script type="text/javascript" src="../lib/layui/layui.all.js" charset="utf-8"></script>
    <script type="text/javascript" src="../js/jquery.min.js" charset="utf-8"></script>
    <script type="text/javascript" src="../js/xadmin.js"></script>
    <script type="text/javascript" src="../js/commonutil.js"></script>
    <script src="./function.js" charset="utf-8"></script>
</head>
<body>
<div class="edit-form-class" style="padding-top: 20px;">
    <div class="layui-fluid">
        <div class="layui-row">
            <form class="layui-form" id="addTemplateForm">
                <div class="layui-form-item">
                    <div class="layui-row">
                        <label class="layui-form-label">*模板编号</label>
                        <div class="layui-input-inline" style="padding-top: 12px;width: 95px">
                            <span id="templateNoPrefix" style="color:#030303"></span>
                        </div>
                        <div class="layui-input-inline">
                            <input type="text" id="templateNo" name="templateNo"
                                   lay-verify="required|templateNoVerify"
                                   style="width: 245px" autocomplete="off" placeholder="" maxlength="50" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">*模板名称</label>
                    <div class="layui-input-block">
                        <input type="text" id="templateName" name="templateName"
                               lay-verify="required|templateNameVerify"
                               style="width: 350px" autocomplete="off" placeholder="" maxlength="50" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">*模板来源</label>
                    <div class="layui-input-block">
                        <input type="radio" name="templateResource" lay-filter="templateListen" value="1" title="附件上传"
                               checked>
                        <input type="radio" name="templateResource" lay-filter="templateListen" value="2" title="线上编辑">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">*模板文件</label>
                    <div class="layui-input-block" style="padding-top: 5px">
                        <span id="fileName" style="color:#999"></span>
                        <button type="button" class="layui-btn layui-btn-normal" id="uploadFile">
                            上传文件
                        </button>
                        </br></br>
                        <span style="color:#999">只能上传ftl格式文件，文件不能超过100kb</span>
                        <input id='templateExtId' name='templateExtId' type="text" class="text" maxlength='50'
                               style='display:none;'/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">*模板文件</label>
                    <div class="layui-input-block" style="padding-top: 5px">
                        <textarea id="templateEdit" name="templateEdit" style="display: none;"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">签章类型</label>
                    <div class="layui-input-block" style="width: 350px">
                        <select name="sealType" lay-filter="sealType" id="sealType"
                                lay-verify="required|sealTypeVerify">
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">公章规则</label>
                    <div class="layui-input-block" id="officialSealId" style="width: 350px">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label" style="padding-left: 15px;">*配置审批流</label>
                    <div class="layui-input-block" id="approval"></div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">备注</label>
                    <textarea placeholder="请输入内容" name="remark" id="remark" style="width: 400px" maxlength="200" lay-verify="remarksVerify"
                              class="layui-textarea"></textarea>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <div style="padding-left: 120px;padding-top: 20px;">
                            <div class="layui-btn" lay-submit="" autocomplete="off" lay-filter="submitBtn"
                                    id="submitBtn">保存
                            </div>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
</body>

<script>
    var index;
    jQuery(function () {
        layui.use(['laypage', 'jquery', 'layer', 'table', 'form', 'util', 'upload', 'layedit'], function () {
            let form = layui.form;
            let upload = layui.upload;
            let layedit = layui.layedit;
            //建立编辑器
            index = layedit.build('templateEdit');
            //隐藏文本编辑器
            hideLayEdit(form);
            //初始化渠道
            queryChannels(form);
            //初始化签章类型
            querySealType(form);
            //radio监听
            radioListener(form);
            //select监听
            selectListener(form);
            //获取模板编号前缀
            queryTemplateNoPrefix(form);
            //文件上传
            let uploadUrl = serverPre + "/manager/scs/v2/template/upload";
            uploadFile(upload, uploadUrl);
            form.render();
        });
    });

    //提交
    $('#submitBtn').click(function () {
        var approverObj = approval.result();
        console.log(approverObj)
        let url = serverPre + "/manager/scs/v2/template/save";
        let formData = serializeObject($("#addTemplateForm"));
        let officialSealId = [];
        $("#officialSealId").find('input:checkbox').each(function (i, item) {
            if ($(this).is(':checked')) {
                officialSealId.push(item.value);
            }
        });
        layui.use(['jquery', 'layedit'], function () {
            let layedit = layui.layedit;
            let fileData = layedit.getText(index);
            //签章类型校验
            if (formData.sealType !== "1") {
                if (officialSealId.length === 0) {
                    alert("签章类型不能为空！");
                    return false;
                }
            }
            //是否上传模板校验
            if (formData.templateExtId === "" && fileData === "") {
                alert("请上传模板！");
                return false;
            }
            let commitData = JSON.stringify({
                "templateNo": $('#templateNoPrefix').html()+formData.templateNo,
                "templateName": formData.templateName,
                "fileData": fileData,
                "refId": formData.templateExtId,
                "resource": formData.templateResource,
                "sealType": formData.sealType,
                "remark": formData.remark,
                "officialSealId": officialSealId,
                "userInfoRos": approverObj.userInfos,
                "approverIds": approverObj.approvers
            });
            submitForm(commitData, url);
        })
    });
    /**
     * 配置审批流
     */
    $(function () {
        $("#approval").load("../approval/approvalConfig.html", "", function () {
            approval = initSubmitApproval("开始");
        })
    })
</script>
</html>
